/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pt.mleiria.datastructures;

import pt.mleiria.utils.RndGenerator;

/**
 *
 * @author manuel
 */
public class ThreeSum {
    
    public static int count(int[] a){
        final int N = a.length;
        int count = 0;
        for (int i = 0; i < N; i++){
            for (int j = i+1; j < N; j++){
                for (int k = j+1; k < N; k++){
                    if(a[i] + a[j] + a[k] == 0){
                        count++;
                    }
                }
            }
        }
        return count;
    }
    
    public static void main(String[] args){
        //int[] a = {8, 30, -40, -20, -10, 40, 0, 10, 5};
        
        int[] a250 = RndGenerator.genArrInts(-500, 500, 250);
        int[] a500 = RndGenerator.genArrInts(-500, 500, 500);
        int[] a1000 = RndGenerator.genArrInts(-500, 500, 1000);
        int[] a2000 = RndGenerator.genArrInts(-500, 500, 2000);
        int[] a4000 = RndGenerator.genArrInts(-500, 500, 4000);
        int[] a8000 = RndGenerator.genArrInts(-500, 500, 8000);
        int[] a16000 = RndGenerator.genArrInts(-500, 500, 16000);
        run(a250, "250");
        run(a500, "500");
        run(a1000,"1000");
        run(a2000, "2000");
        run(a4000, "4000");
        run(a8000, "8000");
        run(a16000, "16000");
        
    }
    
    private static void run(int[] a, String n){
        StopWatch st = new StopWatch();
        System.out.println("Triplets found:["+ count(a) + "]");
        System.out.println("[" + n + ";" + st.elapsedTime() + "]");
    }
    
}
